package com.share.four.netty.heartBeat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import com.share.four.netty.marshalling.util.MarshallingCodeUtil;

public class Client {
	
	public static void main(String[] args) {
		
		//client端不用bossGroup
		NioEventLoopGroup workerGroup = new NioEventLoopGroup();
		
		try {
			Bootstrap b = new Bootstrap();
			b.group(workerGroup);
			b.channel(NioSocketChannel.class);
			b.option(ChannelOption.SO_KEEPALIVE, true);
			b.handler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(MarshallingCodeUtil.buildMarshallingDecoder());
					ch.pipeline().addLast(MarshallingCodeUtil.buildMarshallingEncoder());
					ch.pipeline().addLast(new ClientHandler());
				}
			});
			
			ChannelFuture f = b.connect("127.0.0.1", 12345).sync();
			
			
			//等待直到connet被关闭
			f.channel().closeFuture().sync();
			
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			workerGroup.shutdownGracefully();
		}
	}

}
